#!/usr/bin/perl -w
use strict;
use Benchmark;

use Getopt::Long;

my $usage = <<"USAGE";

Usage:
    extract_regex
	--search="\d+"
	--every=1000
	--verbose
	--help
		file.txt...
		
USAGE

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#	Get options

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
# mandatory


# optional parameters
my $every = 1000;
my $search;
my $verbose;
my $help;
GetOptions(
			'every=i'		=> \$every		,
			'search=s'		=> \$search		,
			'verbose' 		=> \$verbose	,
			'help'			=> \$help);

die $usage if ($help);
die "\nERROR:\n\tMissing Argument: search\n" .$usage
						  unless ($search);



die "\nERROR:\n\tNo files specified\n" .$usage
						  unless (@ARGV);
	
	my $cntlines = 0;
	my $cntregex = 0;
	while (<>)
	{
		if (my @arr = /$::opt_search/g)
		{
			print join "\n", @arr, "" if (@arr);
			$cntregex += @arr;
		}
		++$cntlines;
		print STDERR "$cntlines, $cntregex\n" if ($::opt_every &&
														($cntlines % $::opt_every == 0));
	}
	
	print STDERR "\t$cntregex\titems in $cntlines lines.\n" if $::opt_verbose;
}	


#	my $t = timeit(1, 'mainfunc()');
#    print STDERR "Counting took: ",timestr($t),"\n";
mainfunc();